package com.qianjiangcloud.store.activity.account;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alipay.client.PayResult;
import com.qianjiangcloud.store.AgentApplication;
import com.qianjiangcloud.store.BaseDoFragment;
import com.qianjiangcloud.store.Run;
import com.qianjiangcloud.store.activity.AgentActivity;
import com.qianjiangcloud.store.http.JsonRequestBean;
import com.qianjiangcloud.store.http.JsonTask;
import com.qianjiangcloud.store.http.JsonTaskHandler;
import com.qianjiangcloud.store.ui.pulltorefresh.lib.PullToRefreshBase.OnRefreshListener;
import com.qianjiangcloud.store.ui.pulltorefresh.lib.PullToRefreshListView;
import com.qianjiangcloud.store.util.ChooseUtils;
import com.qianjiangcloud.store.util.LogUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import com.qianjiangcloud.store.R;
import com.qianjiangcloud.store.libary.cache.MyImageLoader;
import com.qianjiangcloud.store.wxapi.WXPayEntryActivity;

import cn.shopex.pay.AliPayHandle;
import cn.shopex.pay.AliPayInfo;
import cn.shopex.pay.WeChatPayHandle;
import cn.shopex.pay.WeiXinPayInfo;

public class AccountTotalOrdersFragment extends BaseDoFragment implements WXPayEntryActivity.PayStateCallBack, AliPayHandle.PayResultListener {
    private final int REQUEST_CODE_ORDER_DETAIL = 0x1001;
    private final int REQUEST_CODE_ORDER_RATING = 0x1002;
    private final int REQUEST_CODE_ORDER_RECOMMEND = 0x1003;
    private PullToRefreshListView mOrdersListView;

    private RelativeLayout mTotalOrderAll;
    private RelativeLayout mTotalOrderPaying;
    private RelativeLayout mTotalOrderShipping;
    private RelativeLayout mTotalOrderReceiving;
    private RelativeLayout mTotalOrderRecommend;
    private RelativeLayout mSelectView;
    private BaseAdapter mOrdersListAdapter;
    private boolean isRecommend = false;
    private int pageNum = 0;
    private JsonTask mTask;
    private LayoutInflater mInflater;
    private TextView mHintText;
    private JSONObject mPayingOrder;
    private WeChatPayHandle weChatPayHandle;

    private boolean mShowAllOrders = false; // 全部订单

    private String mPayStatus = "0";
    private String mPayStatusKey = "pay_status";
    private int mOrderState = 0;
    private Dialog dialog;
    boolean isLoadingData;
    boolean isLoadEnd;
    private ArrayList<JSONObject> mOrderArray = new ArrayList<JSONObject>();
    private boolean isOffline = false;
    private JSONObject data;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActionBar.setTitle(R.string.account_orders_title);
        Intent data = mActivity.getIntent();
        mOrderState = data.getIntExtra(Run.EXTRA_VALUE, 0);
    }

    @Override
    public void init(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
        super.init(inflater, container, savedInstanceState);
        mInflater = inflater;
        rootView = inflater.inflate(R.layout.fragment_total_orders_main, null);
        mTotalOrderAll = (RelativeLayout) findViewById(R.id.taotal_orders_all);
        mTotalOrderPaying = (RelativeLayout) findViewById(R.id.taotal_orders_paying);
        mTotalOrderShipping = (RelativeLayout) findViewById(R.id.taotal_orders_shipping);
        mTotalOrderReceiving = (RelativeLayout) findViewById(R.id.taotal_orders_receiving);
        mTotalOrderRecommend = (RelativeLayout) findViewById(R.id.taotal_orders_recommend);
        mOrdersListView = (PullToRefreshListView) findViewById(R.id.taotal_orders_listview);
        mHintText = (TextView) findViewById(R.id.total_orders_hint);
        mTotalOrderAll.setOnClickListener(mSaleClickListener);
        mTotalOrderPaying.setOnClickListener(mSaleClickListener);
        mTotalOrderShipping.setOnClickListener(mSaleClickListener);
        mTotalOrderReceiving.setOnClickListener(mSaleClickListener);
        mTotalOrderRecommend.setOnClickListener(mSaleClickListener);
        Run.removeFromSuperView(mHintText);
        mHintText.setLayoutParams(new AbsListView.LayoutParams(mHintText
                .getLayoutParams()));
        mOrdersListView.getRefreshableView().addFooterView(mHintText);
        mOrdersListAdapter = new OrderAdapter();
        mOrdersListView.getRefreshableView().setAdapter(mOrdersListAdapter);
        mOrdersListView.getRefreshableView().setOnItemClickListener(
                new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {

                    }

                });
        mOrdersListView.setOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
                    mOrdersListAdapter.notifyDataSetChanged();
                } else {
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {
                if (totalItemCount < 3 || isLoadingData || isLoadEnd)
                    return;
                // 滚动到倒数第3个时，自动加载下一页
                if (totalItemCount - (firstVisibleItem + visibleItemCount) <= 3)
                    loadNextPage(pageNum, false);

            }
        });
        mOrdersListView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                loadNextPage(0, false);
            }

            @Override
            public void onRefreshMore() {
            }
        });

        if (mOrderState == R.id.account_orders_paying) {
            mShowAllOrders = false;
            mPayStatus = "0";
            mPayStatusKey = "pay_status";
            mSelectView = mTotalOrderPaying;
        } else if (mOrderState == R.id.account_orders_shipping) {
            mShowAllOrders = false;
            mPayStatus = "0";
            mPayStatusKey = "ship_status";
            mSelectView = mTotalOrderShipping;
        } else if (mOrderState == R.id.account_orders_receiving) {
            mShowAllOrders = false;
            mPayStatus = "1";
            mPayStatusKey = "ship_status";
            mSelectView = mTotalOrderReceiving;
        } else if (mOrderState == R.id.account_orders_recommend) {
            mSelectView = mTotalOrderRecommend;
            isRecommend = true;
        } else {
            mSelectView = mTotalOrderAll;
            mShowAllOrders = true;
        }
        mSelectView.setSelected(true);
        mSelectView.getChildAt(1).setVisibility(View.VISIBLE);
        loadNextPage(pageNum, true);

    }

    private OnClickListener mSaleClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            mHintText.setVisibility(View.GONE);
            mSelectView.setSelected(false);
            mSelectView.getChildAt(1).setVisibility(View.GONE);
            if (v == mTotalOrderAll) {
                mTask.cancel(true);
                isRecommend = false;
                mShowAllOrders = true;
                mSelectView = mTotalOrderAll;
            } else if (v == mTotalOrderPaying) {
                mTask.cancel(true);
                isRecommend = false;
                mShowAllOrders = false;
                mPayStatus = "0";
                mPayStatusKey = "pay_status";
                mSelectView = mTotalOrderPaying;
            } else if (v == mTotalOrderShipping) {
                mTask.cancel(true);
                isRecommend = false;
                mShowAllOrders = false;
                mPayStatus = "0";
                mPayStatusKey = "ship_status";
                mSelectView = mTotalOrderShipping;
            } else if (v == mTotalOrderReceiving) {
                mTask.cancel(true);
                isRecommend = false;
                mShowAllOrders = false;
                mPayStatus = "1";
                mPayStatusKey = "ship_status";
                mSelectView = mTotalOrderReceiving;
            } else if (v == mTotalOrderRecommend) {
                mTask.cancel(true);
                mTask.isCancelled();
                isRecommend = true;
                mShowAllOrders = false;
                mSelectView = mTotalOrderRecommend;
            }
            mSelectView.setSelected(true);
            mSelectView.getChildAt(1).setVisibility(View.VISIBLE);
            loadNextPage(0, true);
        }
    };

    private void loadNextPage(int oldPageNum, boolean isShowdialog) {
        //任务是否完成
        if (isLoadingData) {
            return;
        }
        this.pageNum = oldPageNum + 1;
        if (this.pageNum == 1) {
            isLoadEnd = false;
            mOrderArray.clear();
            mHintText.setVisibility(View.GONE);
            mOrdersListAdapter.notifyDataSetChanged();
            if (!isShowdialog)
                mOrdersListView.setRefreshing();
        }
        mTask = new JsonTask();
        if (isRecommend) {
            Run.excuteJsonTask(mTask, new GetRecommendOrdersTask(isShowdialog));
        } else {
            Run.excuteJsonTask(mTask, new GetOrdersTask(isShowdialog));
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
    }

    @Override
    public void onResume() {
        super.onResume();
        boolean pay = Run.loadOptionBoolean(mActivity, "WXPayResult", false);
        if (pay) {
            Run.savePrefs(mActivity, "WXPayResult", false);
            if (Run.loadOptionBoolean(mActivity, "PayResult", true)) {
                loadNextPage(0, true);
            }
        }
    }

    @Override
    public void ui(int what, Message msg) {
        switch (msg.what) {
            case SDK_PAY_FLAG: {// 支付宝支付结果
                PayResult payResult = new PayResult((String) msg.obj);

                // 支付宝返回此次支付结果及加签，建议对支付宝签名信息拿签约时支付宝提供的公钥做验签
                String resultInfo = payResult.getResult();

                String resultStatus = payResult.getResultStatus();

                // 判断resultStatus 为“9000”则代表支付成功，具体状态码代表含义可参考接口文档
                if (TextUtils.equals(resultStatus, "9000")) {
                    loadNextPage(0, true);
                } else {
                    // 判断resultStatus 为非“9000”则代表可能支付失败
                    // “8000”代表支付结果因为支付渠道原因或者系统原因还在等待支付结果确认，最终交易是否成功以服务端异步通知为准（小概率状态）
                    if (TextUtils.equals(resultStatus, "8000")) {
                        Toast.makeText(mActivity, "支付结果确认中", Toast.LENGTH_SHORT)
                                .show();

                    } else {
                        // 其他值就可以判断为支付失败，包括用户主动取消支付，或者系统返回的错误
                        Toast.makeText(mActivity, "支付失败", Toast.LENGTH_SHORT)
                                .show();
                    }
                }
                break;

            }
        }
    }


    private class OrderAdapter extends BaseAdapter implements OnClickListener {

        @Override
        public int getCount() {
            return mOrderArray.size();
        }

        @Override
        public JSONObject getItem(int position) {
            return mOrderArray.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }


        class ViewHolder {
            public TextView textNumber;
            public View goDetailView;
            public LinearLayout content;
            public TextView goodNum;
            public TextView totalAmount;
            public Button buttonCancelComplete;
            public Button buttonCancel;
            public Button buttonLogistics;
            public Button buttonAffirm;
            public Button buttonPay;
            public TextView statusTextView;
            public TextView expressinfo;


        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            boolean isStatus = false;

            ViewHolder holder = null;

            //优化订单数据显示重复问题--2016-5-4 jelly

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.fragment_orders_item,
                        null);
                holder = new ViewHolder();


                holder.textNumber = (TextView) convertView
                        .findViewById(R.id.account_orders_item_number);
                holder.goDetailView = convertView
                        .findViewById(R.id.account_orders_item_go_detail);
                holder.content = (LinearLayout) convertView
                        .findViewById(R.id.account_orders_item_goods);
                holder.goodNum = ((TextView) convertView
                        .findViewById(R.id.account_orders_item_goods_num));
                holder.totalAmount = ((TextView) convertView
                        .findViewById(R.id.account_orders_item_price));

                holder.statusTextView = ((TextView) convertView
                        .findViewById(R.id.account_orders_item_status));

                holder.buttonPay = (Button) convertView
                        .findViewById(R.id.account_orders_item_pay);
                holder.buttonAffirm = (Button) convertView
                        .findViewById(R.id.account_orders_item_affirm);
                holder.buttonLogistics = (Button) convertView
                        .findViewById(R.id.account_orders_item_logistics);
                holder.buttonCancel = (Button) convertView
                        .findViewById(R.id.account_orders_item_cancel);
                holder.buttonCancelComplete = (Button) convertView
                        .findViewById(R.id.account_orders_item_cancel_complete);
                holder.expressinfo = ((TextView) convertView
                        .findViewById(R.id.account_orders_item_freight));

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            JSONObject all = getItem(position);

            JSONObject object = all.optJSONObject("payinfo");
            String ordersNum = all.optString("order_id");
            List<JSONObject> mGoodsArrayList = new ArrayList<JSONObject>();

            String totalmount = all.optString("total_amount");
            holder.totalAmount.setText(Run
                    .buildString("￥", totalmount));

            holder.textNumber.setText(all.optString("order_id"));
            holder.goodNum.setText(all.optString("goods_num"));

            JSONObject expressInfo = all.optJSONObject("shipping");
            holder.expressinfo.setText(expressInfo.optString("cost_shipping") + "元");


            holder.goDetailView.setTag(all);
            holder.goDetailView.setOnClickListener(this);

            holder.buttonPay.setOnClickListener(this);
            holder.buttonAffirm.setOnClickListener(this);
            holder.buttonCancel.setOnClickListener(this);
            holder.buttonCancelComplete.setOnClickListener(this);
            holder.buttonLogistics.setOnClickListener(this);
            holder.buttonPay.setTag(all);
            holder.buttonCancel.setTag(all);
            holder.buttonAffirm.setTag(all);
            holder.buttonLogistics.setTag(all);
            holder.buttonCancelComplete.setTag(all);
            if ("dead".equalsIgnoreCase(all.optString("status"))) {
                holder.statusTextView.setText(R.string.orders_orders_cancel);
                holder.buttonCancelComplete.setVisibility(View.GONE);
                holder.buttonPay.setVisibility(View.GONE);
                holder.buttonAffirm.setVisibility(View.GONE);
                holder.buttonCancel.setVisibility(View.GONE);
                holder.buttonLogistics.setVisibility(View.GONE);
            } else if ("finish".equalsIgnoreCase(all.optString("status"))) {
                // statusTextView.setText(R.string.orders_complete);
                // isRecommend = true;
                if (isRecommend) {
                    isStatus = true;
                    holder.statusTextView.setText(R.string.orders_recommend);
                } else {
                    if ("0".equals(all.optString("is_opinions"))) {
                        isStatus = true;
                        holder.statusTextView.setText(R.string.orders_recommend);
                    } else {
                        holder.statusTextView.setText(R.string.orders_complete);
                    }
                }
                holder.buttonLogistics.setVisibility(View.VISIBLE);
                holder.buttonPay.setVisibility(View.GONE);
                holder.buttonAffirm.setVisibility(View.GONE);
                holder.buttonCancel.setVisibility(View.GONE);
                holder.buttonCancelComplete.setVisibility(View.GONE);
            } else if (all.optInt("ship_status") == 1) {
                holder.statusTextView.setText(R.string.account_orders_state_receive);
                holder.buttonLogistics.setVisibility(View.VISIBLE);
                holder.buttonPay.setVisibility(View.GONE);
                holder.buttonAffirm.setVisibility(View.VISIBLE);
                holder.buttonCancel.setVisibility(View.GONE);
                holder.buttonCancelComplete.setVisibility(View.GONE);
            } else if (all.optInt("pay_status") == 0) {
                holder.statusTextView.setText(R.string.account_trade_paying);
                holder.buttonLogistics.setVisibility(View.GONE);
                holder.buttonAffirm.setVisibility(View.GONE);
                holder.buttonCancel.setVisibility(View.VISIBLE);
                holder.buttonCancelComplete.setVisibility(View.GONE);

                JSONObject payinfo = all.optJSONObject("payinfo");
                if ("offlinecard".equals(payinfo.opt("pay_app_id"))) {
                    holder.buttonPay.setVisibility(View.GONE);
                } else {
                    holder.buttonPay.setVisibility(View.VISIBLE);
                }
                //如果是线下支付那么我的订单不显示支付按钮
                if (object.optString("display_name").equals("线下支付")) {
                    holder.buttonPay.setVisibility(View.GONE);
                    isOffline = true;
                }

                //如果是货到付款，隐藏支付按钮
                if (object.optString("display_name").equals("货到付款")) {
                    holder.buttonPay.setVisibility(View.GONE);
                }
            } else if (all.optInt("ship_status") == 0) {
                holder.statusTextView.setText(R.string.account_orders_state_shipping);
                holder.buttonLogistics.setVisibility(View.GONE);
                //如果是货到付款，显示取消按钮
                if (object.optString("display_name").equals("货到付款")) {
                    holder.buttonCancel.setVisibility(View.VISIBLE);
                } else {
                    holder.buttonCancel.setVisibility(View.GONE);
                }
                holder.buttonPay.setVisibility(View.GONE);
                holder.buttonAffirm.setVisibility(View.GONE);
                holder.buttonCancelComplete.setVisibility(View.GONE);
            } else {
                holder.buttonLogistics.setVisibility(View.GONE);
                holder.buttonPay.setVisibility(View.GONE);
                holder.buttonAffirm.setVisibility(View.GONE);
                holder.buttonCancel.setVisibility(View.GONE);
                holder.buttonCancelComplete.setVisibility(View.VISIBLE);
            }

            JSONArray goodsArray = all.optJSONArray("goods_items");

            if (goodsArray != null && goodsArray.length() > 0) {
                for (int i = 0; i < goodsArray.length(); i++)
                    mGoodsArrayList.add(goodsArray.optJSONObject(i));
                GoodsListAdapter adapter = new GoodsListAdapter(
                        mGoodsArrayList, isStatus, ordersNum, all.optString("status"));
                holder.content.removeAllViews();
                for (int i = 0; i < mGoodsArrayList.size(); i++) {
                    holder.content.addView(adapter.getView(i, null, null));
                }
            }

            return convertView;
        }

        @Override
        public void onClick(View v) {
            final JSONObject order = (JSONObject) v.getTag();
            if (v.getId() == R.id.account_orders_item_pay) {
                JSONObject payinfo = order.optJSONObject("payinfo");
                //线下就让支付按钮失效
                if ("offline".equals(payinfo.optString("pay_app_id"))) {
                    return;
                }

                JsonRequestBean bean = new JsonRequestBean(
                        "mobileapi.paycenter.dopayment")
                        .addParams("payment_order_id",
                                order.optString("order_id"))
                        .addParams("payment_cur_money",
                                order.optString("total_amount"))
                        .addParams("payment_pay_app_id",
                                payinfo.optString("pay_app_id"));
                Run.excuteJsonTask(new JsonTask(), new BalancePayTask(bean));
             /*   //支付按钮进去订单详情页支付
                AgentApplication.getApp(mActivity).setOrderDetail(order);
                startActivityForResult(
                        AgentActivity.intentForFragment(mActivity,
                                AgentActivity.FRAGMENT_GOODS_ORDERS_DETAIL)
                                .putExtra(Run.EXTRA_DETAIL_TYPE, isRecommend),
                        REQUEST_CODE_ORDER_DETAIL);*/

            } else if (v.getId() == R.id.account_orders_item_logistics) {
                // 物流
                startActivity(AgentActivity.intentForFragment(mActivity,
                        AgentActivity.FRAGMENT_GOODS_LOGISTICS).putExtra("orderId", order.optString("order_id")));

            } else if (v.getId() == R.id.account_orders_item_affirm) {
                Run.excuteJsonTask(new JsonTask(),
                        new CompleteOrdersTask(order.optString("order_id")));

            } else if (v.getId() == R.id.account_orders_item_cancel) {

                if (isOffline)//如果是线下支付那么，就是提示订单不能取消
                {
                    dialog = AccountLoginFragment.showAlertDialog(mActivity, "该订单处于线下支付已锁定，不可以取消！", -1, -1, null, null, false, null);
                } else {
                    // 取消
                    dialog = AccountLoginFragment.showAlertDialog(mActivity,
                            "是否确定取消该订单？", "取消", "确定", new OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    dialog.dismiss();
                                }
                            }, new OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    dialog.dismiss();
                                    String id = order.optString("order_id");
                                    new JsonTask().execute(new CancelOrderTask(id,
                                            order));
                                }
                            }, false, null);
                }

            } else {

                AgentApplication.getApp(mActivity).setOrderDetail(order);
                startActivityForResult(
                        AgentActivity.intentForFragment(mActivity,
                                AgentActivity.FRAGMENT_GOODS_ORDERS_DETAIL)
                                .putExtra(Run.EXTRA_DETAIL_TYPE, isRecommend),
                        REQUEST_CODE_ORDER_DETAIL);
            }

        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK)
            return;
        if (requestCode == REQUEST_CODE_ORDER_DETAIL) {
            loadNextPage(0, true);
        } else if (requestCode == REQUEST_CODE_ORDER_RATING) {
            loadNextPage(0, true);
        } else if (requestCode == REQUEST_CODE_ORDER_RECOMMEND) {
            loadNextPage(0, true);
        }
    }

    private class GetOrdersTask implements JsonTaskHandler {
        boolean isTrue;

        public GetOrdersTask(boolean isTrue) {
            this.isTrue = isTrue;
        }

        @Override
        public JsonRequestBean task_request() {
            if (isTrue)
                showCancelableLoadingDialog();
            JsonRequestBean bean = new JsonRequestBean(
                    "mobileapi.member.orders");
            if (!mShowAllOrders && !TextUtils.isEmpty(mPayStatus)
                    && !TextUtils.isEmpty(mPayStatusKey)) {
                bean.addParams(mPayStatusKey, mPayStatus);
            }
            bean.addParams("n_page", String.valueOf(pageNum));
            return bean;
        }

        @Override
        public void task_response(String json_str) {
            hideLoadingDialog();
            mOrdersListView.onRefreshComplete();
            try {
                JSONObject all = new JSONObject(json_str);
                if (Run.checkRequestJson(mActivity, all)) {
                    JSONArray array = all.optJSONArray("data");
                    for (int i = 0, count = array.length(); i < count; i++)
                        mOrderArray.add(array.getJSONObject(i));
                    if (mOrderArray.size() <= 0) {
                        mHintText.setVisibility(View.VISIBLE);
                    } else {
                        mHintText.setVisibility(View.GONE);
                    }
                    mOrdersListAdapter.notifyDataSetChanged();
                }
            } catch (Exception e) {
            }
        }
    }

    private class GoodsListAdapter extends BaseAdapter implements
            OnClickListener {
        private String orderId;
        private List<JSONObject> mGoodsArray;
        private boolean isRecommend;

        public GoodsListAdapter(List<JSONObject> goodsArray,
                                boolean isRecommend, String ordersNum, String orderId) {
            mGoodsArray = goodsArray;
            this.isRecommend = isRecommend;
            this.orderId = orderId;
        }

        @Override
        public int getCount() {
            if (mGoodsArray != null) {
                return mGoodsArray.size();
            }
            return 0;
        }

        @Override
        public JSONObject getItem(int position) {

            return mGoodsArray.get(position);
        }

        @Override
        public long getItemId(int position) {

            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = mInflater.inflate(
                        R.layout.fragment_orders_goods_item, null);
            }
            JSONObject goods = getItem(position);
            if (goods != null) {
                JSONObject product = goods.optJSONObject("product");
                if (product != null) {
                    View view = convertView
                            .findViewById(R.id.account_orders_item_recommend);
                    ImageView goodsImage = (ImageView) convertView
                            .findViewById(R.id.account_orders_item_thumb);
                    TextView goodsTiTextView = (TextView) convertView
                            .findViewById(R.id.account_orders_item_title);
                    TextView goodsQuantityextView = (TextView) convertView
                            .findViewById(R.id.account_orders_item_quantity);
                    TextView goodsPricetView = (TextView) convertView
                            .findViewById(R.id.account_orders_item_price);
                    TextView specView = (TextView) convertView.findViewById(R.id.account_orders_item_summary);

                    // LinearLayout goodsRationgLinear = (LinearLayout)
                    // convertView
                    // .findViewById(R.id.account_orders_goods_rating_comple);
                    LinearLayout goodsRecommend = (LinearLayout) convertView
                            .findViewById(R.id.account_orders_goods_recommend);

                    View recommendView = convertView
                            .findViewById(R.id.account_orders_goods_recommend);
                    View ratingView = convertView
                            .findViewById(R.id.account_orders_goods_ratings);
                    recommendView.setTag(product);
                    recommendView.setTag(R.id.about_tel, orderId);
                    ratingView.setTag(product);
                    ratingView.setTag(R.id.about_tel, orderId);
                    goodsImage.setTag(product);
                    recommendView.setOnClickListener(this);
                    ratingView.setOnClickListener(this);
                    goodsImage.setOnClickListener(this);
                    if (isRecommend) {
                        view.setVisibility(View.VISIBLE);
                        if ("finish".equals(orderId)) {
                            //去评价隐藏
                            // ratingView.setVisibility(View.VISIBLE);
                            goodsRecommend.setVisibility(View.VISIBLE);
                        } else {
                            ratingView.setVisibility(View.GONE);
                            goodsRecommend.setVisibility(View.GONE);
                        }
                    } else {
                        view.setVisibility(View.GONE);
                    }
        /*            mVolleyImageLoader.showImage(goodsImage,
                            product.optString("thumbnail_pic_src"));*/
                    MyImageLoader.displayDefaultImage(product.optString("thumbnail_pic_src"), goodsImage);

                    goodsTiTextView.setText(product.optString("name"));
                    goodsQuantityextView.setText(Run.buildString("x",
                            product.optString("quantity")));
                    goodsPricetView.setText(product.optString("price"));
                    JSONObject prodeces = product.optJSONObject("products");
                    if (!prodeces.isNull("spec_info")) {
                        specView.setText(prodeces.optString("spec_info"));
                    }

                }
            }
            return convertView;
        }

        @Override
        public void onClick(View v) {
            String mOrderId = (String) v.getTag(R.id.about_tel);
            JSONObject googds = (JSONObject) v.getTag();
            if (v.getId() == R.id.account_orders_goods_ratings) {
                startActivityForResult(
                        AgentActivity
                                .intentForFragment(mActivity,
                                        AgentActivity.FRAGMENT_ORDERS_RATING)
                                .putExtra(Run.EXTRA_DATA, googds.toString())
                                .putExtra(Run.EXTRA_ADDR, googds.optString("order_id")),
                        REQUEST_CODE_ORDER_RATING);
            } else if (v.getId() == R.id.account_orders_goods_recommend) {
                ChooseUtils chooseUtils = new ChooseUtils();
                chooseUtils.setOrder_id(googds.optString("order_id"));
                String goods_id = googds.optString("goods_id");
                chooseUtils.setGoods_id(goods_id);
                String goods_name = googds.optString("goods_name");
                chooseUtils.setGoods_name(goods_name);
                String brand_name = googds.optString("brand_name");
                chooseUtils.setBrand_name(brand_name);
                String image = googds.optString("image");
                chooseUtils.setImagePath(image);
                Bundle bundle = new Bundle();
                bundle.putSerializable(
                        mActivity.getString(R.string.intent_key_serializable),
                        chooseUtils);
                Intent intent = AgentActivity.intentForFragment(mActivity,
                        AgentActivity.FRAGMENT_GOODS_SHOOSEG);
                intent.putExtras(bundle);
                startActivity(intent);
            } else if (v.getId() == R.id.account_orders_item_thumb) {
                String goodsIID = googds.optString("goods_id");
                Intent intent = AgentActivity.intentForFragment(mActivity,
                        AgentActivity.FRAGMENT_GOODS_DETAIL).putExtra(
                        Run.EXTRA_CLASS_ID, goodsIID);
                mActivity.startActivity(intent);
            }

        }
    }

    private class CancelOrderTask implements JsonTaskHandler {
        private String orderId;
        private JSONObject data;

        public CancelOrderTask(String orderID, JSONObject order) {
            orderId = orderID;
            this.data = order;
        }

        @Override
        public void task_response(String json_str) {
            hideLoadingDialog_mt();
            try {
                JSONObject all = new JSONObject(json_str);
                if (Run.checkRequestJson(mActivity, all)) {
                    Run.alertL(mActivity,
                            R.string.account_orders_canceled_order_ok);
                    this.data.put("status", "dead");
                    loadNextPage(0, true);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public JsonRequestBean task_request() {
            showCancelableLoadingDialog();
            JsonRequestBean req = new JsonRequestBean(
                    "mobileapi.member.cancel");
            req.addParams("order_id", orderId);
            return req;
        }

    }

    private class BalancePayTask implements JsonTaskHandler {
        private JsonRequestBean bean;
        private WeiXinPayInfo weiXinPayInfo;

        public BalancePayTask(JsonRequestBean bean) {
            this.bean = bean;
        }

        @Override
        public JsonRequestBean task_request() {
            showCancelableLoadingDialog();
            return bean;
        }

        @Override
        public void task_response(String json_str) {
            hideLoadingDialog_mt();
            try {
                JSONObject all = new JSONObject(json_str);
                if (Run.checkRequestJson(mActivity, all)) {
                    mPayingOrder = all;

                    JSONObject data = all.optJSONObject("data");
                    AccountTotalOrdersFragment.this.data = data;
                    JSONObject obj = data.optJSONObject("order");

                    String payAppId = data.optString("pay_app_id");

                    //防止数据获取不正常
                    if (TextUtils.isEmpty(payAppId))
                        payAppId = obj.optString("pay_app_id");

                    LogUtils.instance.d("test", "111111111111111");
                    if ("malipay".equals(payAppId)) {
                        AliPayInfo aliPayInfo = new AliPayInfo();
                        aliPayInfo.setPartner(data.optString("mer_id"));
                        aliPayInfo.setSeller(data.optString("seller_account_name"));
                        aliPayInfo.setRsa_private(data.optString("key"));
                        aliPayInfo.setNotify_url(data.optString("callback_url"));
                        aliPayInfo.setSubject(data.optString("pay_object"));
                        aliPayInfo.setBody(data.optString("body"));
                        aliPayInfo.setPrice(data.optString("total_amount"));
                        aliPayInfo.setOrder_id(data.optString("payment_id"));
                        try {
                            AliPayHandle aliPayHandle = new AliPayHandle(getActivity(), aliPayInfo);
                            aliPayHandle.setPayResultListener(AccountTotalOrdersFragment.this);
                        } catch (AliPayHandle.APliPaySetingInfoNullException e) {
                            e.printStackTrace();
                        }
                        return;
                    }
                    if ("deposit".equals(payAppId)) {
                        if (obj.optString("pay_status").equals("1")) {
                            Toast.makeText(getActivity(), "支付成功", Toast.LENGTH_LONG).show();
                            startActivity(AgentActivity.intentForFragment(mActivity, AgentActivity.FRAGMENT_ACCOUNT_ORDERS)
                                    .putExtra(Run.EXTRA_VALUE, 0));
                        } else {
                            Toast.makeText(mActivity, data.optString("msg").toString(), Toast.LENGTH_LONG).show();
                        }
                        return;

                    } else if ("wxpaysdk".equals(payAppId)) {//sdk支付
                        // 微信
                        WXPayEntryActivity.setPayStateCallBack(AccountTotalOrdersFragment.this);
                        weChatPayHandle = new WeChatPayHandle(getActivity());
                        weiXinPayInfo = new WeiXinPayInfo();
                        weiXinPayInfo.setOrderid(obj.optString("order_id"));
                        weiXinPayInfo.setPay_app_id(obj.optString("pay_app_id"));
                        weiXinPayInfo.setTotal_amount(data.optString("total_amount"));
                        weiXinPayInfo.setMethod("mobileapi.pay.weixinpay");
                        weiXinPayInfo.setToken(Run.TOKEN);
                        weiXinPayInfo.setUrl(Run.API_URL);
                        weChatPayHandle.callWXPay(weiXinPayInfo);
                        return;

                    }
                } else {
                    Run.startThirdPartyPayment(mActivity, all);
                }
            } catch (Exception e) {

                LogUtils.instance.d("test", "3333333333333333");
            }
        }
    }

    private class GetRecommendOrdersTask implements JsonTaskHandler {
        boolean isTrue;


        public GetRecommendOrdersTask(boolean isTrue) {
            this.isTrue = isTrue;
        }

        @Override
        public JsonRequestBean task_request() {
            isLoadingData = true;
            if (isTrue) {
                showCancelableLoadingDialog();
            }
            JsonRequestBean bean = new JsonRequestBean(
                    "mobileapi.member.get_order_opinions");
            bean.addParams("n_page", String.valueOf(pageNum));
            return bean;
        }

        @Override
        public void task_response(String json_str) {
            hideLoadingDialog();
            isLoadingData = false;
            mOrdersListView.onRefreshComplete();
            try {
                JSONObject all = new JSONObject(json_str);
                if (Run.checkRequestJson(mActivity, all)) {
                    JSONArray array = all.optJSONArray("data");
                    //如果这个data取值为空那么说明，没有数据了，再继续取值没有意义
                    if (array.length() == 0) {
                        //到这里为止数据全部加载完毕
                        isLoadEnd = true;
                    }
                    for (int i = 0, count = array.length(); i < count; i++)
                        mOrderArray.add(array.getJSONObject(i));
                    if (mOrderArray.size() <= 0) {
                        mHintText.setVisibility(View.VISIBLE);
                    } else {
                        mHintText.setVisibility(View.GONE);
                    }
                    mOrdersListAdapter.notifyDataSetChanged();
                }
            } catch (Exception e) {
            }
        }
    }

    private class CompleteOrdersTask implements JsonTaskHandler {
        private String OrderId;

        public CompleteOrdersTask(String OrderId) {
            this.OrderId = OrderId;
        }

        public JsonRequestBean task_request() {
            if (pageNum == 1)
                showCancelableLoadingDialog();
            JsonRequestBean bean = new JsonRequestBean(
                    "mobileapi.order.dofinish");
            bean.addParams("order_id", OrderId);
            return bean;
        }

        @Override
        public void task_response(String json_str) {
            hideLoadingDialog_mt();
            mOrdersListView.onRefreshComplete();
            try {
                JSONObject all = new JSONObject(json_str);
                if (Run.checkRequestJson(mActivity, all)) {
                    dialog = AccountLoginFragment.showAlertDialog(mActivity,
                            "确认收货成功！", "", "OK", null, new OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    dialog.dismiss();
                                    loadNextPage(0, true);
                                }
                            }, false, null);
                }
            } catch (Exception e) {
            }
        }
    }


    @Override
    public void weixinPayResult(boolean state) {
        if (state) {
            JsonRequestBean bean = new JsonRequestBean("mobileapi.paycenter.confirm_pay_status")
                    .addParams("pay_app_id", "wxpaysdk")
                    .addParams("payment_id", weChatPayHandle.getPaymentid());
            Run.excuteJsonTask(new JsonTask(), new CheckPayTask(bean));

        } else {
            loadNextPage(0, true);
        }
    }

    @Override
    public void aliPayResult(cn.shopex.pay.client.PayResult payResult) {
        // 支付宝支付结果回调
        String resultStatus = payResult.getResultStatus();

        // 判断resultStatus 为“9000”则代表支付成功，具体状态码代表含义可参考接口文档
        if (TextUtils.equals(resultStatus, "9000")) {
            Toast.makeText(mActivity, "支付成功", Toast.LENGTH_SHORT).show();
//                    getActivity().finish();
        } else {
            // 判断resultStatus 为非“9000”则代表可能支付失败
            // “8000”代表支付结果因为支付渠道原因或者系统原因还在等待支付结果确认，最终交易是否成功以服务端异步通知为准（小概率状态）
            if (TextUtils.equals(resultStatus, "8000")) {
                Toast.makeText(mActivity, "支付结果确认中", Toast.LENGTH_SHORT).show();

            } else {
                // 其他值就可以判断为支付失败，包括用户主动取消支付，或者系统返回的错误
                Toast.makeText(mActivity, "支付失败", Toast.LENGTH_SHORT).show();
                // startActivity(AgentActivity.intentForFragment(mActivity,
                // AgentActivity.FRAGMENT_ACCOUNT_ORDERS).putExtra(
                // Run.EXTRA_VALUE, 0));
            }
        }
    }

    private class CheckPayTask implements JsonTaskHandler {
        private JsonRequestBean bean;

        public CheckPayTask(JsonRequestBean bean) {
            this.bean = bean;
        }

        @Override
        public JsonRequestBean task_request() {
            showCancelableLoadingDialog();
            return bean;
        }

        @Override
        public void task_response(String json_str) {
            hideLoadingDialog_mt();
            JSONObject all = null;
            try {
                all = new JSONObject(json_str);
                if (null != json_str) {
                    Log.d("test", "检查订单状态或余额支付" + json_str);
                    JSONObject data = all.optJSONObject("data");

                    if (data.optString("status").equals("1")) {
                        Toast.makeText(mActivity, "支付成功", Toast.LENGTH_LONG).show();
                        loadNextPage(0, true);
                    } else {
                        Toast.makeText(mActivity, "支付失败", Toast.LENGTH_LONG).show();
                    }
                }

            } catch (JSONException e) {
                Toast.makeText(mActivity, "支付失败", Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }
        }
    }
}
